package com.bdsoft.actions.resourceManage.Service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.bdsoft.actions.resourceManage.DTO.ResourceDTO;
import com.bdsoft.actions.resourceManage.Dao.ResourceMapper;
import com.bdsoft.actions.resourceManage.Service.ResourceService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    ResourceMapper resourceMapper;

    @Override
    public PageInfo<Map<String,Object>> getResourceList(int pageNum, int pageSize, String resourceId, String resourceName) {
        PageHelper.startPage(pageNum, pageSize);
        List<Map<String,Object>> resList = resourceMapper.selectResourceByParams(resourceId, resourceName);
        return new PageInfo<>(resList);
    }

    @Override
    public Map<String, Object> saveOrUpdateResource(ResourceDTO resourceDTO) {
        if (StrUtil.isBlank(resourceDTO.getResourceId())) {
            resourceDTO.setResourceId(UUID.randomUUID().toString());
            int result = resourceMapper.addResource(resourceDTO);
            return result > 0 ? MapUtil.<String,Object>builder()
                    .put("code", 200)
                    .put("data", "添加资源成功")
                    .build()
                : MapUtil.<String,Object>builder()
                    .put("code", 500)
                    .put("data", "添加资源失败")
                    .build();
        } else {
            int result = resourceMapper.updateResourceById(resourceDTO);
            return result > 0 ? MapUtil.<String,Object>builder()
                    .put("code", 200)
                    .put("data", "修改资源成功")
                    .build()
                : MapUtil.<String,Object>builder()
                    .put("code", 500)
                    .put("data", "修改资源失败")
                    .build();
        }
    }

    @Override
    public Map<String, Object> deleteResourceById(String resourceId) {
        int result = resourceMapper.deleteResourceById(resourceId);
        resourceMapper.removeResourceRoleById(resourceId);

        return result > 0 ? MapUtil.<String,Object>builder()
                .put("code", 200)
                .put("data", "删除资源成功")
                .build()
            : MapUtil.<String,Object>builder()
                .put("code", 500)
                .put("data", "删除资源失败(或者找不到资源)")
                .build();
    }

    @Override
    public Map<String, Object> getResourceListByRole(String roleId) {
        return MapUtil.<String,Object>builder()
                .put("code", 200)
                .put("data", resourceMapper.selectResourceByRoleId(roleId))
                .build();
    }

    @Override
    public Map<String, Object> assignResourceToRole(String roleId, String resourceId) {

        List<String> resourceIds=StrUtil.split(resourceId,",");
        int result=0;

        for (String s:resourceIds){
            resourceMapper.removeResourceFromRole(roleId,s);
            result+=resourceMapper.assignResourceToRole(roleId, s);

        }



        return result > 0 ? MapUtil.<String,Object>builder()
                .put("code", 200)
                .put("data", "分配资源成功")
                .build()
            : MapUtil.<String,Object>builder()
                .put("code", 500)
                .put("data", "分配资源失败")
                .build();
    }

    @Override
    public Map<String, Object> removeResourceFromRole(String roleId, String resourceId) {
        int result = resourceMapper.removeResourceFromRole(roleId, resourceId);
        return result > 0 ? MapUtil.<String,Object>builder()
                .put("code", 200)
                .put("data", "移除资源成功")
                .build()
            : MapUtil.<String,Object>builder()
                .put("code", 500)
                .put("data", "移除资源失败")
                .build();
    }
} 